package com._520it.crm.service.impl;

import com._520it.crm.domain.Employee;
import com._520it.crm.domain.Reviseinfo;
import com._520it.crm.mapper.OrderBillMapper;
import com._520it.crm.mapper.ReviseinfoMapper;
import com._520it.crm.service.IReviseinfoService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

@Service
public class ReviseinfoServiceImpl implements IReviseinfoService {
    @Autowired
    private ReviseinfoMapper reviseinfoMapper;
    @Autowired
    private OrderBillMapper orderBillMapper;


    @Override
    public int insert(Reviseinfo record) {
        return reviseinfoMapper.insert(record);
    }

    @Override
    public int updateByPrimaryKey(Reviseinfo record) {
        return reviseinfoMapper.updateByPrimaryKey(record);
    }


    public void insert(Reviseinfo info, int tempNo, int orderBillStatus) {
        //查询数据库中是否有该保单的批改记录, 如果有就更新该记录
        Long orderBillId = info.getOrderBillId();
        Reviseinfo reviseinfo =  reviseinfoMapper.selectByOrderBillId(orderBillId);
            if(info.getReviseid() == null ){
                info.setReviseid((long) UUID.randomUUID().hashCode());
            }
            if(tempNo == 0 && orderBillStatus == 7 ){
                //提交审核
                //更改保单状态 为批改待审核
                orderBillMapper.updateStatus(info.getOrderBillId(),orderBillStatus);
                info.setTemp(Reviseinfo.TEMP_NO);
            }else{
                //保存为暂存单
                info.setTemp(Reviseinfo.TEMP_YES);
            }
            Employee auditor = (Employee) SecurityUtils.getSubject().getPrincipal();
            info.setAuditor(auditor);
        if(reviseinfo == null){
            reviseinfoMapper.insert(info);
        }else{
            reviseinfoMapper.updateByOrderBillId(info);
        }
    }

    @Override
    public Map<String, Object> selectReviseinfo(Long orderBillId) {

        return reviseinfoMapper.selectReviseinfo(orderBillId);
    }
}
